<template>
  <div class="login">
    <el-card class="login-card">
      <!-- 用户名密码登录 -->
      <div class="account-login" v-if="loginType === 'account'">
        <h2 class="title">后台app管理系统登录</h2>
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名：">
            <el-input
              v-model="ruleForm.username"
              autocomplete="off"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item label="密码：">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- QRcode登录 -->
      <div class="qrcode-login" v-else>
        <div class="qrcode-login-title">
            <h2>请扫码</h2>
            <p>请使用微信扫码下方二维码</p>
        </div>
        <el-image class="qrcode-img" :src="qrcode"></el-image>
      </div>

      <!-- 切换按钮 -->
       <div class="change-btn" @click="changeLoginType()">
        <el-image class="change-btn-qrcode" :src="qrcode" v-if="loginType==='account'"></el-image>
        <el-image class="change-btn-computer" :src="computer" v-else></el-image>
       </div>

       <!-- 遮挡 -->
        <div class="mask"></div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from "vue";
import qrcode from "@/assets/img/qrcode.png"
import computer from  "@/assets/img/computer.png"
import { useRouter } from "vue-router";

const router = useRouter();
const ruleFormRef = ref(null);
const ruleForm = ref(
    {
        username: "",
        password: ""
    }
);
const loginType = ref("account");

function submitForm(formRef) {
    if(!formRef) return;
    formRef.validate( valid => {
        if(valid && (ruleForm.value.username === 'admin' && ruleForm.value.password === '123')) {
            router.push('/dashboard');
        } else {
            console.log("error submit!!");
            return false;
        }
    }

    );
}

function changeLoginType() {
    loginType.value = loginType.value === "account" ? "qrconde" : "account";
}
</script>

<style scoped>
.title {
    z-index: 2;
}
.login-card {
  width: 400px;
  height: 400px;
  margin: 50px auto;
  position: relative;
}
.account-login, .qrcode-login {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.el-button {
    width: 300px;
}
.qrcode-img {
    width: 200px;
    height: 200px;
}
.qrcode-login-title {
    text-align: center;
}
.change-btn {
    background: rgb(134, 134, 233);
    width: 50px;
    height: 50px;
    position: absolute;
    top: 0;
    right: 0;
    cursor: pointer;
}
.change-btn-qrcode, .change-btn-computer {
    width: 30px;
    height: 30px;
    margin: 5px 5px 5px 10px;
}
.mask {
    background: white;
    width: 70px;
    height: 70px;
    position: absolute;
    transform: rotate(45deg);
    top:15px;
    right: 15px;
}
</style>